package banco.composEstoque;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import utilidades.FormatarData;
import banco.Banco;
import dominio.Cliente;
import dominio.composEstoque.*;
import fachada.composEstoque.Fachada_Dem_Vendas;

public class Demonstrativo_Vendas_DAO implements Fachada_Dem_Vendas {

	/* (non-Javadoc)
	 * @see banco.composEstoque.Fachada_Dem_Vendas#insertDem_Vendas(dominio.Cliente, dominio.composEstoque.Demonstrativo_Vendas)
	 */
	@SuppressWarnings("finally")
	public void insertDem_Vendas(Cliente c, Demonstrativo_Vendas dem_vendas, String filial) throws ClassNotFoundException, IOException {
	        
	        Connection conn = null;
	        PreparedStatement stmt = null;
	        ResultSet rs = null;
	        
	        try {
	            conn = Banco.getConexaoEXTRATOPAES();
	            stmt = conn.prepareStatement("insert into composestoque_dem_vendas values (?, ?, ?, ?, ?, ?, ?)");
	            stmt.setString(1, FormatarData.formatar(dem_vendas.getData()));
	            stmt.setString(2, dem_vendas.getCfop().getCodigo().trim());
	            stmt.setFloat(3, dem_vendas.getVendas());
	            stmt.setString(4, dem_vendas.getIcms().getDescricao().trim());
	            stmt.setFloat(5, dem_vendas.getIcms().getValor());
	            stmt.setString(6, c.getCnpj().trim());
	            stmt.setString(7, filial);
	            stmt.executeUpdate();
	        }
	        catch (SQLException ex) {
	            ex.printStackTrace();
	        }
	        
	        catch (Exception e) {
	        	e.printStackTrace();
	        }
	        finally {
		        Banco.closeConexao(conn, rs, stmt);
	        }
	    }
	
	@SuppressWarnings("finally")
	public Collection selectDem_Vendas(Cliente c, String filial) throws IOException {
		
		Connection conn = null;
	    PreparedStatement stmt = null;
	    ResultSet rs = null;
	    Demonstrativo_Vendas dv;
	    Collection<Demonstrativo_Vendas> res = new ArrayList<Demonstrativo_Vendas>();
	    
	    try {
	        conn = Banco.getConexaoEXTRATOPAES();
	        stmt = conn.prepareStatement("select * from composestoque_dem_vendas where cnpj_cliente = ? and filial = ?");
	        stmt.setString(1, c.getCnpj().trim());
	        stmt.setString(2, filial);
	        rs = stmt.executeQuery();
	        while (rs.next()){
	        	dv = new Demonstrativo_Vendas();
	        	CFOP cfop = new CFOP();
	        	cfop.setCodigo(rs.getString("codigo"));
	        	dv.setCfop(cfop);
	        	dv.setData_string(rs.getString("data"));
	        	dv.setData(FormatarData.inserirData(rs.getString("data")));
	        	dv.setPeriodo(FormatarData.getMesAno(rs.getString("data")));
	        	dv.setVendas(rs.getFloat("vendas"));
	        	dv.setIcms(new ICMS(rs.getString("descricao_icms"), rs.getFloat("valor_icms")));
	        	dv.setId(rs.getInt("id"));
	        	res.add(dv);
	        }
	    }
	    catch (SQLException ex) {
	        ex.printStackTrace();
	    }
	    finally {
	        Banco.closeConexao(conn, rs, stmt);
	        return res;
	    }
	    
	}
	
}
